$chr = $ARGV[0];
open(BUFF,"gunzip -c beagle_out_$chr.snps_chr$chr.bgl.gz.phased.gz |") or die "no file $chr phased found\n";
$line = <BUFF>;
$line = <BUFF>;


open(TPED,"gunzip -c ../EUR.chr$chr.20110521.b37p0.snps.markers.gz |") or die "no file $chr tped found\n";
open(WRBUFF,">result_chr$chr.tped") or die " no file exists\n";
while(<TPED>)
{
	chomp($_);
	@snp = split(" ",$_);
	$line = <BUFF>;
	@array = split(" ",$line);
	#print "$array[1] $snp[0]\n";
	
	if($array[1] ne $snp[0])
	{
		die " $array[1] ne $snp[0]\n";
	}
	shift(@array);
	shift(@array);
	$line =join(" ",@array);
		print WRBUFF "$chr $snp[0] 0 $snp[1] $line\n";
}
system("gunzip -c snps_chr$chr.tfam.gz > result_chr$chr.tfam");
system("plink --tfile result_chr$chr --recode --out result_chr$chr");
